package com.ff.discounts.component.tab.charge

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.ff.discounts.R
import com.ff.discounts.component.rankinglist.RankingListActivity
import com.ff.discounts.component.tab.home.HistoryData
import com.ff.discounts.databinding.FragmentSquareBinding
import com.ff.discounts.databinding.ItemNavBinding
import com.ff.discounts.fragment.BaseViewModelFragment
import com.ff.discounts.library.log.HiLog
import com.ff.discounts.superui.decoration.GridDividerItemDecoration
import com.ff.discounts.superui.util.DensityUtil
import com.ff.discounts.util.DeviceUtils
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

/**
 *广场
 * @author: fufang
 * @date: 2023/10/17
 */
class SquareFragment : BaseViewModelFragment<FragmentSquareBinding>(){

    private val recommendAdapter by lazy {
        RecommendAdapter()
    }

    private val hotAdapter by lazy {
        RecommendAdapter()
    }

    override fun initDatum() {
        super.initDatum()

        //绑定adapter
        binding.recommendList.adapter = recommendAdapter
        recommendAdapter.submitList(SquareFragment.RecommendDataList)
        hotAdapter.submitList(SquareFragment.HotList)
        binding.hotList.adapter = hotAdapter



        showData()
        // nav 导航
        for(i in 0 until navDatum.size){
            ItemNavBinding.inflate(layoutInflater).apply {
                content.text = navDatum[i].name
                icon.setImageResource(navDatum[i].src)
                binding.navContainer.addView(root)
                root.setOnClickListener{
                    HiLog.d("${navDatum[i].name}")
                    when(i){

                        1 -> {
                            startActivity(RankingListActivity::class.java)
                        }
                    }
                }

            }
        }
    }

    override fun initViews() {
        super.initViews()

        val statsH : Int = DeviceUtils.getNavigationBarHeight(requireContext())
        binding.fragmentSquare.setPadding(0, statsH,0,0);

        // 设置我的追剧的布局管理器
        val layoutPlayManager = GridLayoutManager(hostActivity, 3)
        binding.recommendList.layoutManager = layoutPlayManager
        val itemDecoration = GridDividerItemDecoration(binding.recommendList.context,
            DensityUtil.dip2px(binding.recommendList.context,5f).toInt())
        binding.recommendList.addItemDecoration(itemDecoration)

        // 设置热门推荐的布局管理器
        val hotLayoutPlayManager = GridLayoutManager(hostActivity, 3)
        binding.hotList.layoutManager = hotLayoutPlayManager
        binding.hotList.addItemDecoration(itemDecoration)


    }

    override fun initListeners() {
        super.initListeners()

    }

    private fun showData(){
        //轮播图
        binding.banner.setAdapter(object : BannerImageAdapter<Int>(swiperDatum){
            override fun onBindView(
                holder: BannerImageHolder?,
                data: Int,
                position: Int,
                size: Int
            ) {
                // 图片加载自己实现
                // 设置本地图片
                holder?.imageView?.setImageResource(data)

            }
        }).isAutoLoop(true).addBannerLifecycleObserver(this).indicator = CircleIndicator(requireContext())
    }

    companion object{
        fun newInstance(): SquareFragment {
            val args = Bundle()
            val fragment = SquareFragment()
            fragment.arguments = args
            return fragment
        }

        private val swiperDatum = mutableListOf<Int>(
            R.mipmap.follow_banner,
            R.mipmap.follow_banner,
            R.mipmap.follow_banner
        )

        private val navDatum = mutableListOf<NavItem>(
            NavItem(R.drawable.nav1,"追剧"),
            NavItem(R.drawable.nav2,"热门"),
            NavItem(R.drawable.nav3,"领币"),
            NavItem(R.drawable.nav4,"我的")
        )

        private val RecommendDataList = mutableListOf<HistoryData>(
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集")
        )

        private val HotList = mutableListOf<HistoryData>(
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo,"极品校花爱上我", "第五集")
        )

    }

}